// pages/blind_date/report.js
import {config} from "../../config/config";
import {UserAuth} from "../../model/user-auth";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    active_type: '头像非本人获取无法看清正面',
    user_auth_id: 0,
    content_length: 0,
    content: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        user_auth_id: options.user_auth_id,
      })
  },
  afterRead(event) {
    const { file } = event.detail;
    const self = this
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: config.apiFileUrl, // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      success(res) {
        const fileData = JSON.parse(res.data)
        // 上传完成需要更新 fileList
        const fileList = self.data.fileList;
        fileList.push({ ...file, url: fileData[0].url, img_id: fileData[0].id });
        self.setData({ fileList });
      },
    });
  },
  /**
   * 更换类型
   */
  changeType(e) {
    this.setData({
      active_type: e.currentTarget.dataset.title
    })
  },
  delImg(e) {
    const index = e.detail.index;
    let fileList = this.data.fileList;
    fileList.splice(index, 1);
    this.setData({
      fileList: fileList,
    })
  },
  // 输入活动简介
  changeContent(e) {
    if(e.detail.value.length > 255) {
      return false;
    }
    this.setData({
      content: e.detail.value,
      content_length: e.detail.value.length,
    })
  },
  /**
   * 投诉
   */
  async addReport() {
    const data = {
      fileList: this.data.fileList,
      user_auth_id: this.data.user_auth_id,
      type: this.data.active_type,
      content: this.data.content,
    }
    const res = await UserAuth.addReport(data);
    if(res) {
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
      this.setData({
        fileList: [],
        active_type: '头像非本人获取无法看清正面',
        content_length: 0,
        content: '',
      })
    }
  }
})